#include <iostream>
#include <string>
using namespace std;
int count_int(unsigned long long int a, unsigned long long int b)
{ 
	  int i = 0;
	  if (a > b) return 0;
    while (a/10)
    {
    	int temp1 = a % 10;
    	int temp2 = b % 10;
      
      if (temp1 == temp2){
      	a /= 10;
      b /= 10;
      }
      else return 0;
    }
    
	return 1;
}
int main(int argc, char ** argv) 
{ 
	 unsigned  long long int x = 0, a = 0, b = 0;
	 int  n = 0;
	 cin >> n;
	 while (n)
	 {
	 	cin >> x >> a >>b;
	  int i = 0, count = 0;
	  for (i = a; i <= b; i++)
	    if (count_int(x, i))count++;
	 		cout << count << endl; 
	 	  n--;
	 		}
	 		
}
	
